package com.web.main.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Supplier;
import com.web.main.entity.form.SupplierForm;
import com.web.main.entity.param.SupplierQuery;
import com.web.main.entity.vo.SupplierVo;
import com.web.main.exception.SupplierException;
import com.web.main.mapper.SupplierMapper;
import com.web.main.service.SupplierService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 长顺
 * @since 2020-11-21
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {
    /**
     * 查一个供应商
     * @param supplierId  供应商主键
     * @return 供应商前端对象
     */
    @Override
    public SupplierVo getOneSupplier(String supplierId) {
        Supplier supplier = baseMapper.selectById(supplierId);

        if(ObjectUtils.isEmpty(supplier)) {
            throw new SupplierException("supplier not found with id:" + supplierId);
        }

        //将数据库查出的对象赋给前端对象
        SupplierVo supplierVo = new SupplierVo();
        BeanUtils.copyProperties(supplier, supplierVo);

        return supplierVo;
    }

    /**
     * 添加供应商
     * @param supplierParam
     */
    @Override
    public void addSupplier(SupplierForm supplierParam) {
        if(Objects.isNull(supplierParam)) {
            throw new SupplierException("add supplier failed with supplier is null");
        }

        Supplier supplier = new Supplier();
        //将supplierParam对象的属性拷贝给supplier
        BeanUtils.copyProperties(supplierParam, supplier);
        supplier.setSupplierId(null);

        int insert = baseMapper.insert(supplier);

        if(insert == 0) {
            throw new SupplierException("add supplier failed with supplier:" + supplierParam);
        }
    }

    /**
     * 删除供应商
     * @param supplierId
     */
    @Override
    public void removeSupplierById(String supplierId) {
        int i = baseMapper.deleteById(supplierId);
        if(i == 0) {
            throw new SupplierException("supplier remove failed with id:" + supplierId);
        }
    }


    /**
     * 修改
     * @param supplierParam
     */
    @Override
    public void modifySupplier(SupplierForm supplierParam) {
        if(Objects.isNull(supplierParam)) {
            throw new SupplierException("modify failed with supplier is null");
        }

        String supplierId = supplierParam.getSupplierId();
        if(StringUtils.isBlank(supplierId)) {
            //主键为空，就添加
            addSupplier(supplierParam);
            return;
        }

        String supplierName = supplierParam.getSupplierName();
        String supplierProvinces = supplierParam.getSupplierProvinces();
        Double supplierRechargeMoney = supplierParam.getSupplierRechargeMoney();
        Double supplierServeMoney = supplierParam.getSupplierServeMoney();
        Double supplierParkMoney = supplierParam.getSupplierParkMoney();
        Double supplierOccupyMonty = supplierParam.getSupplierOccupyMonty();
        String supplierContact = supplierParam.getSupplierContact();

        //先查询
        Supplier supplier = baseMapper.selectById(supplierId);

        if(Objects.isNull(supplier)) {
            throw new SupplierException("modify failed with select supplier is null");
        }

        //再修改
        if(!StringUtils.isBlank(supplierName)) {
            supplier.setSupplierName(supplierName);
        }
        if(!StringUtils.isBlank(supplierProvinces)) {
            supplier.setSupplierProvinces(supplierProvinces);
        }
        if(null != supplierRechargeMoney) {
            supplier.setSupplierRechargeMoney(supplierRechargeMoney);
        }
        if(null != supplierRechargeMoney) {
            supplier.setSupplierRechargeMoney(supplierRechargeMoney);
        }
        if(null != supplierServeMoney) {
            supplier.setSupplierServeMoney(supplierServeMoney);
        }
        if(null != supplierParkMoney) {
            supplier.setSupplierParkMoney(supplierParkMoney);
        }
        if(null != supplierOccupyMonty) {
            supplier.setSupplierOccupyMonty(supplierOccupyMonty);
        }
        if(!StringUtils.isBlank(supplierContact)) {
            supplier.setSupplierContact(supplierContact);
        }

        int i = baseMapper.updateById(supplier);

        if(i == 0) {
            throw new SupplierException("modify failed with supplier:" + supplierParam);
        }
    }

    @Override
    public Page<SupplierVo> pageQuery(SupplierQuery supplierQuery) {
        Integer page = supplierQuery.getPage();
        Integer limit = supplierQuery.getLimit();
        //分页查，查电桩状态：不是审批和删除的
        Page<Supplier> supplierPage = new Page<>(page, limit);

        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();

        Page<SupplierVo> supplierVoPage = new Page<>();
        if(ObjectUtils.isEmpty(supplierQuery)) {
            baseMapper.selectPage(supplierPage, queryWrapper);
            BeanUtils.copyProperties(supplierPage, supplierVoPage);
            return supplierVoPage;
        }

        String name = supplierQuery.getName();
        String address = supplierQuery.getAddress();

        if(!StringUtils.isBlank(name)) {
            queryWrapper.like("supplier_name", name);
        }
        if(!StringUtils.isBlank(address)) {
            queryWrapper.like("supplier_provinces", address);
        }

        baseMapper.selectPage(supplierPage, queryWrapper);
        BeanUtils.copyProperties(supplierPage, supplierVoPage);
        return supplierVoPage;
    }


}


